package Pokerstats;

import java.util.Iterator;

/**
 * Summarizes session data.
 * @author Oskari Miettinen
 * @version 1.0, 25.4.2007
 */
public class Summary {
  Player player;
  double netwin;
  double grosswin;
  double grossloss;
  double currentstreak;
  double beststreak;
  double worststreak;
  double hoursplayed;
  double hourlyrate;
  double bbhr;
  
  public Summary(Player player) {
    this.player = player;
 }

  /**
   * Iterates through all session data and calculates
   * summaries.
   *
   */
  public void summarize() {
	netwin = grosswin = grossloss = currentstreak = beststreak =
		worststreak = hoursplayed = hourlyrate = bbhr = 0.0;
    Session session;
    double final_balance = 0.0;
    double bb = 0.0;
    double bb_this = 0.0;
    
    for (Iterator<Session> iter = player.sessionsIterator(); iter.hasNext();) {
      session = iter.next();
      // big bets
      // Now we can't calculate big bets if a game was deleted.
      try { // TODO: If a game is deleted, also delete all sessions accordingly?
        bb_this = player.getGameByID(session.getGameid()).getBigBet();
        if ( bb_this >= 0 )
          bb += bb_this; 
      } catch (NullPointerException ex) {
      }
      final_balance = session.getFinal_balance();
      hoursplayed += session.getHours();
      bbhr = bb / hoursplayed;
      if ( final_balance >= 0 ) {
        if ( currentstreak >= 0 )
          currentstreak += final_balance;
        else
          currentstreak = final_balance;
        if ( currentstreak > beststreak )
          beststreak = currentstreak;
        grosswin += final_balance;
      }
      else { // final_balance < 0
        if ( currentstreak < 0 )
          currentstreak += final_balance;
        else
          currentstreak = final_balance;
        if ( currentstreak < worststreak )
          worststreak = currentstreak;
        grossloss += final_balance;
      }
      netwin += final_balance;
    }
    hourlyrate = netwin / hoursplayed;
  }
  
  public double getNetwin() {return netwin;}
  public double getGrosswin() {return grosswin;}
  public double getGrossloss() {return grossloss;}
  
  public double getCurrentstreak() {return currentstreak;}
  public double getWorststreak() {return worststreak;}
  public double getBeststreak() {return beststreak;}

  public double getHoursplayed() {return hoursplayed;}
  public double getHourlyrate() {return hourlyrate;}
  public double getBBhr() {return bbhr;}
  
}
